package com.yanqu.road.server.gameplayer.module.union;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.entity.union.parergon.UnionParergon;
import com.yanqu.road.entity.union.parergon.UnionParergonOperateResult;
import com.yanqu.road.entity.union.parergon.UnionParergonPlayer;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.Parergon;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.union.UnionParergonMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

public class UnionParergonModule extends GeneralModule {

    private UnionParergon parergon;

    private UnionParergonPlayer parergonPlayer;

    public UnionParergonModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            return;
        }
        parergon = UnionParergonMgr.getParergon(unionUid);
        parergonPlayer = UnionParergonMgr.getParergonPlayer(player.getUserId());

        sync();
    }

    public void sync() {
        if (parergon != null) {
            syncParergon();
            syncParergonPlayer();
            syncBussinessman();
        }
    }


    /**
     * 经营
     * @param parergonId
     * @return
     */
    public UnionParergonOperateResult operate(int parergonId) {
        UnionParergonOperateResult result = new UnionParergonOperateResult();
        calOperateTimes();
        if (parergonPlayer.getOperateTimes() <= 0) {
            result.setRet(GameErrorCode.E_PARERGON_OPERATE_NOT_TIMES);
            result.setOperateTimes(parergonPlayer.getOperateTimes());
            result.setLastRestoreTime(parergonPlayer.getLastRestoreTime());
            return result;
        }
        UserInfo userInfo = player.getUserInfo();
        String unionUid = userInfo.getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            result.setRet(GameErrorCode.E_PARERGON_NOT_IN_UNION);
            result.setOperateTimes(parergonPlayer.getOperateTimes());
            result.setLastRestoreTime(parergonPlayer.getLastRestoreTime());
            return result;
        }

        result = UnionParergonMgr.operate(unionUid, parergonId, player);
        if(0 != result.getRet()){
            result.setOperateTimes(parergonPlayer.getOperateTimes());
            result.setLastRestoreTime(parergonPlayer.getLastRestoreTime());
            return result;
        }
        //刷新次数
        parergonPlayer.setOperateTimes(parergonPlayer.getOperateTimes() - 1);
        result.setOperateTimes(parergonPlayer.getOperateTimes());
        result.setLastRestoreTime(parergonPlayer.getLastRestoreTime());

        //增加经营次数
        parergonPlayer.setTodayTimes(parergonPlayer.getTodayTimes() + 1);
        parergonPlayer.setTotalTimes(parergonPlayer.getTotalTimes() + 1);
        parergonPlayer.setHistoryTimes(parergonPlayer.getHistoryTimes() + 1);

        //加奖励
        Property property = new Property(GameConfig.GAME_MONEY_SILVER, result.getAddSilver());
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Union, eLogMoneyType.UnionParergonOperate);

        player.notifyListener(eGamePlayerEventType.ParergonOperateTimes.getValue(), 1);
        player.notifyListener(eGamePlayerEventType.UnionParergonOperateTimes.getValue(), new UnionActivityTaskArgs(UnionActivityTaskArgs.TYPE_ADD_VALUE, 1, player.getUserInfo().getUnionUid()));
        player.notifyListener(eGamePlayerEventType.ParergonTotalOperateTimes.getValue(), parergonPlayer.getHistoryTimes());
        return result;
    }


    public void setOperateResultData(UnionParergonOperateResult result, int code){
        result.setRet(code);
        result.setOperateTimes(parergonPlayer.getOperateTimes()); //剩余经营次数
        result.setLastRestoreTime(parergonPlayer.getLastRestoreTime());
    }


    /**
     * 一键经营
     * @param parergonId
     * @param operateTimes 经营次数
     * @return
     */
    public UnionParergonOperateResult oneKeyOperate(int parergonId, int operateTimes) {
        UnionParergonOperateResult result = new UnionParergonOperateResult();

        UserInfo userInfo = player.getUserInfo();
        String unionUid = userInfo.getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            setOperateResultData(result, GameErrorCode.E_PARERGON_NOT_IN_UNION);
            return result;
        }
        if (parergonPlayer.getOperateTimes() <= 0) {
            setOperateResultData(result, GameErrorCode.E_PARERGON_OPERATE_NOT_TIMES);
            return result;
        }

        result = UnionParergonMgr.oneKeyOperate(parergonPlayer, unionUid, parergonId, player, operateTimes);
        if(0 != result.getRet()){
            setOperateResultData(result, result.getRet());
            return result;
        }

        //刷新玩家可经营次数
        parergonPlayer.setOperateTimes(parergonPlayer.getOperateTimes() - result.getOperateTimes());

        //增加经营次数
        parergonPlayer.setTodayTimes(parergonPlayer.getTodayTimes() + result.getOperateTimes());
        parergonPlayer.setTotalTimes(parergonPlayer.getTotalTimes() + result.getOperateTimes());
        parergonPlayer.setHistoryTimes(parergonPlayer.getHistoryTimes() + result.getOperateTimes());

        //加奖励
        Property property = new Property(GameConfig.GAME_MONEY_SILVER, result.getAddSilver());
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Union, eLogMoneyType.UnionParergonOperate);

        player.notifyListener(eGamePlayerEventType.ParergonOperateTimes.getValue(), result.getOperateTimes());
        player.notifyListener(eGamePlayerEventType.UnionParergonOperateTimes.getValue(), new UnionActivityTaskArgs(UnionActivityTaskArgs.TYPE_ADD_VALUE, result.getOperateTimes(), player.getUserInfo().getUnionUid()));
        player.notifyListener(eGamePlayerEventType.ParergonTotalOperateTimes.getValue(), parergonPlayer.getHistoryTimes());
        result.setFinishTimes(result.getOperateTimes());
        setOperateResultData(result, result.getRet());
        return result;
    }

    /**
     * 计算经营冷却时间
     */
    public void calOperateTimes() {
        if (parergonPlayer != null) {
            long now = System.currentTimeMillis() / 1000;
            long lastRestoreTime = parergonPlayer.getLastRestoreTime();
            int times = (int)(now - lastRestoreTime) / GameConfig.PARERGON_OPERATE_RESTORE_TIME;
            int hasTimes = times + parergonPlayer.getOperateTimes();
            if (hasTimes >= GameConfig.PARERGON_OPERATE_MAX_TIMES) {
                parergonPlayer.setOperateTimes(GameConfig.PARERGON_OPERATE_MAX_TIMES);
                parergonPlayer.setLastRestoreTime(now);
            } else {
                parergonPlayer.setOperateTimes(hasTimes);
                lastRestoreTime += times * GameConfig.PARERGON_OPERATE_RESTORE_TIME;
                parergonPlayer.setLastRestoreTime(lastRestoreTime);
            }
        }
    }

    public void syncParergon() {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            return;
        }
        Parergon.ParergonMsg.Builder msg = UnionParergonMgr.parseParergonMsg(unionUid, false);
        player.sendPacket(Protocol.U_UNION_PARERGON_SYNC, msg);
    }

    public void syncParergonPlayer() {
        //发送之前在同步一次次数
        calOperateTimes();
        Parergon.OperateMsg.Builder msg = Parergon.OperateMsg.newBuilder();
        msg.setOperateTimes(parergonPlayer.getOperateTimes());
        msg.setLastRestoreTime(parergonPlayer.getLastRestoreTime());
        msg.setTodayTimes(parergonPlayer.getTodayTimes());
        msg.setTotalTimes(parergonPlayer.getTotalTimes());
        player.sendPacket(Protocol.U_UNION_PARERGON_OPERATE_SYNC, msg);
    }

    /**
     * 获取行脚商人数据
     */
    public void syncBussinessman() {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            return;
        }
        Integer bussinessId = UnionParergonMgr.getBussinessman(unionUid);
        if (bussinessId != null) {
            Parergon.BusinessmanDataMsg.Builder msg = UnionParergonMgr.parseBusinessmanMsg(bussinessId);
            UnionParergonMgr.setBusinessmanStatusMsg(msg, player.getUserInfo().getNickName());
            player.sendPacket(Protocol.U_UNION_PARERGON_BUSINESSMAN, msg);
        }
    }

    public int getParegonTotalOperateTimes(){
        if(null != parergonPlayer){
            return parergonPlayer.getHistoryTimes();
        }
        return 0;
    }

    public int getOperateTimes() {
        if(null != parergonPlayer){
            return parergonPlayer.getOperateTimes();
        }
        return 0;
    }
}
